#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(this, SIGNAL(sgnShowImage(QImage)), this, SLOT(sltShowImage(QImage)));
    vdVideoDecode.globalInit();
    m_playImages = this->findChildren<PlayImage *>();
}

MainWindow::~MainWindow()
{
    delete ui;
    vdVideoDecode.globalUnInit();
}


void MainWindow::sltShowImage(QImage qimage)
{
    if(vdVideoDecode.isClose())
        return;

    for(int i = 0; i < m_playImages.count(); i++)
    {
        m_playImages.at(i)->updateImage(qimage);
    }
}

void MainWindow::on_pushButtonOpenFile_clicked(bool checked)
{
    QString filename = QFileDialog::getOpenFileName(nullptr, "Open Video File");
   if (!filename.isEmpty())
    {
       //vdVideoDecode.setHWDecoder(true);
       vdVideoDecode.setStreamDecodeCallback([](int nStreamType, int nFormatType, long long llDecodeTs, long long llPlayTs, int width, int height, unsigned char ** pStreamData, int * linesize, void * pUserData){
           MainWindow *pMainWindow = (MainWindow *)pUserData;
           QImage qimage = VideoDecode::ConvertRGB24FrameToQImage(pStreamData[0],width,height);
           emit pMainWindow->sgnShowImage(qimage);
       },this);
       vdVideoDecode.setFileStatusCallback([](int FileStatus, int nErrorCode, void * pUserData){
            qDebug()<<"file is end";
       },this);

       vdVideoDecode.open(filename);
       /*
       if(player.openFile(filename))
       {
          player.show();
       }
       */
    }
}

void MainWindow::on_pushButtonCloseFile_clicked()
{
    vdVideoDecode.close();
}
